CLIP.Search.enc = function(val) {
	return val.replace(/\s/g, "+");
};

CLIP.Search.dec = function(val) {
	return val.replace(/\+/g, ' ');
};

CLIP.Clips.initPage = 1;
CLIP.Clips.currentPage = 1;
CLIP.Clips.pageSize = 20;

CLIP.Clips.searchUrl = function(keyword) {
	// http://phim1v.com/?s=v%E1%BA%A1n+ph%E1%BB%A5ng+chi+v%C6%B0%C6%A1ng&page=2
	return 'http://phim1v.com/?s=' + encodeURIComponent(keyword) + '&page=' + CLIP.Clips.currentPage;
};

CLIP.Clips.filterUrl = function() {
	// http://phim1v.com/danh-sach/phim-chieu-rap/trang-2/?letter=all&order_by=create&order=desc&categories=1&countries=5&years=3
	return 'http://phim1v.com/danh-sach/' + $('#types input:checked').val() + '/trang-' + CLIP.Clips.currentPage + '/?order_by=' + $('#orderbys input:checked').val() + '&categories='
			+ $('#cats input:checked').val();
};

CLIP.Clips.moreHtml = function(text) {
	var html = [];

	text = bum.util.parse(text, '<div id="result-film-ajax">', '</div> </div> </form> </div>');
	if (text == null) {
		console.log("null");
		return html;
	}
	$.each(text.split('<div class="row-fluid clearfix">'), function(i, clip) {
		if (i > 0) {
			$.each(text.split('<div class="col-sm-3'), function(i, clip) {
				if (i > 0) {
					++CLIP.Clips.moreCount;
					var code = bum.util.parse(clip, '<h2 class="title"> <a href="', '"').replace('http://phim1v.com/', '');
					var title = bum.util.parse(clip, '<h2> <a>', '</a>');
					var originalTitle = bum.util.parse(clip, '</a> </h2> <h3>', '</h3>');
					var img = bum.util.parse(clip, '<img src="', '"');
		
					html.push('<a class="item clip" href="#" code="', code, '"><div class="cw">');
					html.push('<img src="http://cdn.bum.vn/_.gif" style="background-image:url(', img, ');"/>');
					html.push('<div class="iw"><h4>', title, '</h4>');
					html.push('<h5>', originalTitle, '</h5></div>');
					html.push('</div></a>');
				}
			});
		}
	});

	return html;
};

CLIP.Clips.headers = [ 'Accept', 'text/plain, text/html', 'Host', 'phim1v.com', 'Cache-Control', 'max-age=0', 'Connection', 'close', 'User-Agent',
               		'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2150.5 Safari/537.36' ].join('|');

CLIP.Info.infoUrl = function(code) {
	// http://phim1v.com/phim-chien-binh-bat-bai/
	return 'http://phim1v.com/' + code;
};

CLIP.Info.parseInfo = function(text) {
	text = bum.util.parse(text, '<div data-role="content">', '<!--HTML compressed,');
	if (text == null) {
		return null;
	}

	var poster = bum.util.parse(text, 'src="', '"');
	var title = bum.util.parse(text, '<h2 class="infoTitlte">', '</h2>');
	var originalTitle = bum.util.parse(text, '<h3 class="small">', '</h3>');
	var detail = bum.util.parse(text, '</h3>', '</div></div><div class="ui-body-c hihi marginT10">');
	if (detail) {
		detail = detail.replace(/<a /g, '<b ').replace(/<\/a>/g, '<\/b>').replace(/http:\/\/phim1v.com\//g, '').replace(/style/g, 'styles');
	}
	var intro = bum.util.parse(text, '<div id="film-info">', '</div> </div></div>');
	
	var chaps = [];
	var eps = bum.util.parse(text, '<div id="servers">', '</div> </div>');
	if(eps == null){
		chaps.push('Hệ thống đang cập nhật dữ liệu...');
	} else {
		$.each(eps.split('<p>'), function(i, slist) {
			if (i > 0) {
				chaps.push('<div><p><b>' + bum.util.parse(slist, '<strong>', '</strong>') + '</b></p></div>');
				$.each(slist.split('<a '), function(i, step) {
					if(i > 0){
						var code = bum.util.parse(step, 'href="', '"').replace(/http:\/\/phim1v.com\//g, '');
						var name = bum.util.parse(step, '">', '</a>');
						chaps.push('<a href="' + code + '">' + name + '</a>');
					}
				});
			}
		});
	}
	
	// return clip
	var clip = {
		poster : poster,
		title : title,
		originalTitle : originalTitle,
		detail : detail,
		chaps : chaps,
		intro : intro
	};

	return clip;
};

CLIP.Chap.headers = [ 'Accept', 'text/plain, text/html', 'Host', 'phim1v.com', 'Cache-Control', 'max-age=0', 'Connection', 'close', 'User-Agent',
		'Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' ].join('|');

CLIP.Chap.chapUrl = function(code) {
	return 'http://phim1v.com/' + code;
};
// custome
CLIP.Chap.streamUrl = function(text) {
	var url = bum.util.parse(text, '<source src="', '"');
	console.log(url);
	return url;
};